package com.hibase.core.bireport.config.fr;


import cn.hutool.json.JSONObject;
import com.fr.cert.token.*;
import com.fr.cert.token.impl.DefaultClaims;
import com.fr.cert.token.impl.DefaultHeader;
import com.fr.cert.token.impl.DefaultJwsHeader;
import com.fr.cert.token.impl.TextCodec;
import com.fr.cert.token.impl.crypto.DefaultJwtSigner;
import com.fr.cert.token.impl.crypto.JwtSigner;
import com.fr.cert.token.lang.Assert;
import com.fr.cert.token.lang.Collections;
import com.fr.cert.token.lang.Objects;
import com.fr.cert.token.lang.Strings;
import com.fr.log.FineLoggerFactory;

import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class DefaultJwtBuilder implements JwtBuilder {
    private Header header;
    private Claims claims;
    private String payload;
    private SignatureAlgorithm algorithm;
    private Key key;
    private byte[] keyBytes;
    private CompressionCodec compressionCodec;

    public DefaultJwtBuilder() {
    }

    public JwtBuilder setHeader(Header var1) {
        this.header = var1;
        return this;
    }

    public JwtBuilder setHeader(Map<String, Object> var1) {
        this.header = new DefaultHeader(var1);
        return this;
    }

    public JwtBuilder setHeaderParams(Map<String, Object> var1) {
        if (!Collections.isEmpty(var1)) {
            Header var2 = this.ensureHeader();
            Iterator var3 = var1.entrySet().iterator();

            while(var3.hasNext()) {
                Entry var4 = (Entry)var3.next();
                var2.put(var4.getKey(), var4.getValue());
            }
        }

        return this;
    }

    protected Header ensureHeader() {
        if (this.header == null) {
            this.header = new DefaultHeader();
        }

        return this.header;
    }

    public JwtBuilder setHeaderParam(String var1, Object var2) {
        this.ensureHeader().put(var1, var2);
        return this;
    }

    public JwtBuilder signWith(SignatureAlgorithm var1, byte[] var2) {
        Assert.notNull(var1, "SignatureAlgorithm cannot be null.");
        Assert.notEmpty(var2, "secret key byte array cannot be null or empty.");
        Assert.isTrue(var1.isHmac(), "Key bytes may only be specified for HMAC signatures.  If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.");
        this.algorithm = var1;
        this.keyBytes = var2;
        return this;
    }

    public JwtBuilder signWith(SignatureAlgorithm var1, String var2) {
        Assert.hasText(var2, "secret key cannot be null or empty.");
        Assert.isTrue(var1.isHmac(), "key bytes may only be specified for HMAC signatures.  If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.");
        byte[] var3 = new byte[0];

        try {
            var3 = var2.getBytes("UTF-8");
        } catch (UnsupportedEncodingException var5) {
            FineLoggerFactory.getLogger().error("jwt key decode with utf-8 error");
        }

        return this.signWith(var1, var3);
    }

    public JwtBuilder signWith(SignatureAlgorithm var1, Key var2) {
        Assert.notNull(var1, "SignatureAlgorithm cannot be null.");
        Assert.notNull(var2, "Key argument cannot be null.");
        this.algorithm = var1;
        this.key = var2;
        return this;
    }

    public JwtBuilder signWithBase64SecretKey(SignatureAlgorithm var1, String var2) {
        Assert.hasText(var2, "base64-encoded secret key cannot be null or empty.");
        Assert.isTrue(var1.isHmac(), "Base64-encoded key bytes may only be specified for HMAC signatures.  If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.");
        byte[] var3 = TextCodec.BASE64.decode(var2);
        return this.signWith(var1, var3);
    }

    public JwtBuilder compressWith(CompressionCodec var1) {
        Assert.notNull(var1, "compressionCodec cannot be null");
        this.compressionCodec = var1;
        return this;
    }

    public JwtBuilder setPayload(String var1) {
        this.payload = var1;
        return this;
    }

    protected Claims ensureClaims() {
        if (this.claims == null) {
            this.claims = new DefaultClaims();
        }

        return this.claims;
    }

    public JwtBuilder setClaims(Claims var1) {
        this.claims = var1;
        return this;
    }

    public JwtBuilder setClaims(Map<String, Object> var1) {
        this.claims = Jwts.claims(var1);
        return this;
    }

    public JwtBuilder addClaims(Map<String, Object> var1) {
        this.ensureClaims().putAll(var1);
        return this;
    }

    public JwtBuilder setIssuer(String var1) {
        if (Strings.hasText(var1)) {
            this.ensureClaims().setIssuer(var1);
        } else if (this.claims != null) {
            this.claims.setIssuer(var1);
        }

        return this;
    }

    public JwtBuilder setSubject(String var1) {
        if (Strings.hasText(var1)) {
            this.ensureClaims().setSubject(var1);
        } else if (this.claims != null) {
            this.claims.setSubject(var1);
        }

        return this;
    }

    public JwtBuilder setAudience(String var1) {
        if (Strings.hasText(var1)) {
            this.ensureClaims().setAudience(var1);
        } else if (this.claims != null) {
            this.claims.setAudience(var1);
        }

        return this;
    }

    public JwtBuilder setExpiration(Date var1) {
        if (var1 != null) {
            this.ensureClaims().setExpiration(var1);
        } else if (this.claims != null) {
            this.claims.setExpiration(var1);
        }

        return this;
    }

    public JwtBuilder setNotBefore(Date var1) {
        if (var1 != null) {
            this.ensureClaims().setNotBefore(var1);
        } else if (this.claims != null) {
            this.claims.setNotBefore(var1);
        }

        return this;
    }

    public JwtBuilder setIssuedAt(Date var1) {
        if (var1 != null) {
            this.ensureClaims().setIssuedAt(var1);
        } else if (this.claims != null) {
            this.claims.setIssuedAt(var1);
        }

        return this;
    }

    public JwtBuilder setId(String var1) {
        if (Strings.hasText(var1)) {
            this.ensureClaims().setId(var1);
        } else if (this.claims != null) {
            this.claims.setId(var1);
        }

        return this;
    }

    public JwtBuilder setDescription(String var1) {
        if (Strings.hasText(var1)) {
            this.ensureClaims().setDescription(var1);
        } else if (this.claims != null) {
            this.claims.setDescription(var1);
        }

        return this;
    }

    public JwtBuilder claim(String var1, Object var2) {
        Assert.hasText(var1, "Claim property name cannot be null or empty.");
        if (this.claims == null) {
            if (var2 != null) {
                this.ensureClaims().put(var1, var2);
            }
        } else if (var2 == null) {
            this.claims.remove(var1);
        } else {
            this.claims.put(var1, var2);
        }

        return this;
    }

    public String compact() {
        if (this.payload == null && Collections.isEmpty(this.claims)) {
            throw new IllegalStateException("Either 'payload' or 'claims' must be specified.");
        } else if (this.payload != null && !Collections.isEmpty(this.claims)) {
            throw new IllegalStateException("Both 'payload' and 'claims' cannot both be specified. Choose either one.");
        } else if (this.key != null && this.keyBytes != null) {
            throw new IllegalStateException("A key object and key bytes cannot both be specified. Choose either one.");
        } else {
            Header var1 = this.ensureHeader();
            Object var2 = this.key;
            if (var2 == null && !Objects.isEmpty(this.keyBytes)) {
                var2 = new SecretKeySpec(this.keyBytes, this.algorithm.getJcaName());
            }

            Object var3;
            if (var1 instanceof JwsHeader) {
                var3 = (JwsHeader)var1;
            } else {
                var3 = new DefaultJwsHeader(var1);
            }

            if (var2 != null) {
                ((JwsHeader)var3).setAlgorithm(this.algorithm.getValue());
            } else {
                ((JwsHeader)var3).setAlgorithm(SignatureAlgorithm.NONE.getValue());
            }

            if (this.compressionCodec != null) {
                ((JwsHeader)var3).setCompressionAlgorithm(this.compressionCodec.getAlgorithmName());
            }

            String var4 = this.base64UrlEncode(var3, "Unable to serialize header to json.");
            String var5;
            if (this.compressionCodec != null) {
                byte[] var6;
                try {
                    var6 = this.payload != null ? this.payload.getBytes(Strings.UTF_8) : this.toJson(this.claims);
                } catch (Exception var9) {
                    throw new IllegalArgumentException("Unable to serialize claims object to json.");
                }

                var5 = TextCodec.BASE64URL.encode(this.compressionCodec.compress(var6));
            } else {
                var5 = this.payload != null ? TextCodec.BASE64URL.encode(this.payload) : this.base64UrlEncode(this.claims, "Unable to serialize claims object to json.");
            }

            String var10 = var4 + '.' + var5;
            if (var2 != null) {
                JwtSigner var7 = this.createSigner(this.algorithm, (Key)var2);
                String var8 = var7.sign(var10);
                var10 = var10 + '.' + var8;
            } else {
                var10 = var10 + '.';
            }

            return var10;
        }
    }

    protected JwtSigner createSigner(SignatureAlgorithm var1, Key var2) {
        return new DefaultJwtSigner(var1, var2);
    }

    protected String base64UrlEncode(Object var1, String var2) {
        byte[] var3 = this.toJson(var1);
        return TextCodec.BASE64URL.encode(var3);
    }

    protected byte[] toJson(Object var1) {
        JSONObject var2 = new JSONObject();
        if (var1 instanceof Map) {
            Map var3 = (Map)var1;
            var2 = new JSONObject(var3);
        }

        return var2.toString().getBytes();
    }
}
